
package coop.bancocredicoop.proyectos.gd

import coop.bancocredicoop.proyectos.gd.FileType
import coop.bancocredicoop.proyectos.gd.DocumentType
import coop.bancocredicoop.proyectos.gd.DocumentTypeController
import coop.bancocredicoop.proyectos.gd.Document

class DocumentTypeControllerTest extends GroovyTestCase {

    void testDocumentTypesAsJSON() {
        def fileType = new FileType(shortName: 'ALTA2', name: 'Alta')
        fileType.save()
        def data1 = [shortName: 'doctypetest',
                     name: 'Doc Type Test',
                     versionable: true,
                     purgeable: true,
                     hasExpiration: true,
                     daysInAdvanceForNotice: 14,
                     defaultLastingMonths: 12,
                     uploadNotificationEmailAddress: null,
                     fileType: fileType]
        def data2 = [shortName: 'doctypetest2',
                     name: 'Doc Type Test 2',
                     versionable: false,
                     purgeable: true,
                     hasExpiration: false,
                     daysInAdvanceForNotice: 5,
                     defaultLastingMonths: 24,
                     uploadNotificationEmailAddress: 'gd@example.com',
                     fileType: fileType]
        def docType1 = new DocumentType(data1)
        def docType2 = new DocumentType(data2)
        docType1.save(flush: true)
        docType2.save(flush: true)
        
        def controller = new DocumentTypeController()
        controller.documentTypesJSON()
        def documentTypes = controller.response.json
                                .get('documentTypes').values()
        assertEquals 2, documentTypes.size()
        if (documentTypes[0].name == data1.name) {
           assertDataSavedWasRetrieved documentTypes[0], data1
           assertDataSavedWasRetrieved documentTypes[1], data2
        } else {
           assertDataSavedWasRetrieved documentTypes[1], data1
           assertDataSavedWasRetrieved documentTypes[0], data2
        }
     } 

    def assertDataSavedWasRetrieved(dataSaved, dataRetrieved) {
        assertEquals dataSaved.name, dataRetrieved.name
        assertEquals dataSaved.purgeable, dataRetrieved.purgeable
        assertEquals dataSaved.hasExpiration, dataRetrieved.hasExpiration
    }

}

